package com.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;

public class FormatSql {

	public static final String formatFile = "D:/moduleeclipse/tip/trunk/db/format.sql";
	public static final String rawFile = "D:/moduleeclipse/tip/trunk/db/data.sql";
	public static final String replaceFile = "D:/moduleeclipse/tip/trunk/db/replace.sql";
	public static final String nonFile = "D:/moduleeclipse/tip/trunk/db/non.sql";

	interface Command {
		public int execute(Param param, StringBuilder result);

		public String getReadFile();

		public String getWriteFile();
	}

	interface Param {
		public int getLength();

		public String getLine();

		public String[] getSplitArray();

		public int getLineNum();
	}

	public class InternalParam implements Param {

		private int length;
		private String line;
		private String[] array;
		private int lineNum;

		public InternalParam(int length, String line, String[] array,
				int lineNum) {
			super();
			this.length = length;
			this.line = line;
			this.array = array;
			this.lineNum = lineNum;
		}

		@Override
		public int getLength() {
			return length;
		}

		@Override
		public String getLine() {
			return line;
		}

		@Override
		public String[] getSplitArray() {
			return array;
		}

		@Override
		public int getLineNum() {
			return lineNum;
		}
	}

	public class Non implements Command {

		String end = ");";
		
		@Override
		public int execute(Param param, StringBuilder result) {
			String line=param.getLine();
			int target=line.indexOf(")");
			String begin=line.substring(0,target);
			result.append(begin).append(",'0'").append(end);
			result.append("\n");
			return 0;
		}

		@Override
		public String getReadFile() {
			return replaceFile;
		}

		@Override
		public String getWriteFile() {
			return nonFile;
		}

	}

	public class Format implements Command {

		private int[] ids = { 9, 10, 11, 12, 14, 16 };
		String begin = "INSERT INTO `jc_model_item` VALUES (";
		String end = ");";

		@Override
		public int execute(Param param, StringBuilder result) {

			int length = param.getLength();
			String line = param.getLine();
			String[] values = param.getSplitArray();

			if (length != 17 && length == 11) {
				result.append(line);
				result.append("\n");
				return -1;
			}
			StringBuilder builder = new StringBuilder(11);
			builder.append(begin);
			boolean contain = false;

			for (int i = 0; i < length; i++) {
				for (int j : ids) {
					if (i + 1 == j) {
						contain = true;
						break;
					}
				}
				if (contain) {
					contain = false;
					continue;
				}
				builder.append(values[i]);

				if (i != length - 1)
					builder.append(",");
			}
			builder.append(end);
			result.append(builder.toString());
			result.append("\n");
			return 1;
		}

		@Override
		public String getReadFile() {
			return rawFile;
		}

		@Override
		public String getWriteFile() {
			return formatFile;
		}
	}

	public class Replace implements Command {

		private int[] ids = { 5 };
		String begin = "INSERT INTO `jc_model_item` VALUES (";
		String end = ");";

		@Override
		public int execute(Param param, StringBuilder result) {
			int length = param.getLength();
			String[] values = param.getSplitArray();

			StringBuilder builder = new StringBuilder(11);
			builder.append(begin);
			boolean contain = false;

			for (int i = 0; i < length; i++) {
				for (int j : ids) {
					if (i + 1 == j) {
						contain = true;
						break;
					}
				}
				if (contain) {
					contain = false;
					builder.append(param.getLineNum());
				} else {
					builder.append(values[i]);
				}

				if (i != length - 1)
					builder.append(",");
			}
			builder.append(end);
			result.append(builder.toString());
			result.append("\n");
			return 1;
		}

		@Override
		public String getReadFile() {
			return formatFile;
		}

		@Override
		public String getWriteFile() {
			return replaceFile;
		}
	}

	public static void main(String[] args) {
		FormatSql sql = new FormatSql();
//		sql.format(sql.new Format());
//		sql.format(sql.new Replace());
		sql.format(sql.new Non());
	}

	public void format(Command command) {

		FileWriter writer = null;
		BufferedReader reader = null;
		try {
			reader = read(command.getReadFile());
			String line;
			int lineNum = 0;
			StringBuilder result = new StringBuilder();
			while ((line = reader.readLine()) != null) {
				lineNum++;
				String newline = line.substring(line.indexOf("(") + 1,
						line.indexOf(")"));
				final String[] values = newline.split(",");
				final int length = values.length;
				command.execute(
						new InternalParam(length, line, values, lineNum),
						result);
			}
			writer = write(result.toString(), command.getWriteFile());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			close(reader, writer);
		}
	}

	private void close(Reader reader, Writer writer) {
		if (reader != null) {
			try {
				reader.close();
			} catch (IOException e) {
			}
		}
		if (writer != null) {
			try {
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private BufferedReader read(String fileName) throws FileNotFoundException {
		BufferedReader buffer = null;
		buffer = new BufferedReader(new FileReader(new File(fileName)));
		return buffer;
	}

	private FileWriter write(String content, String fileName) throws IOException {
		FileWriter writer = new FileWriter(new File(fileName));
		writer.write(content);
		return writer;
	}
}
